// pages/user/shopCar/outshop/outshop.js
import{
  getToken,
  addactivityProduct
}from "../../../../api.js"
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
 
  data: {
    iphoneX:false,
    focus:false,
    showImages:[],
    token_pic:"",
    image:[],
    count:1,
    index:-1,
    number:"",
    ad:true
  },
  val(e){
    this.setData({
      number:e.detail.value,
      ad:true
    })
  },
  our(){
    this.setData({
      ad:false
    })
  },
  img_look(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      current: that.data.showImages[index], // 当前显示图片的http链接
      urls: that.data.showImages // 需要预览的图片http链接列表
    })
  },
  save(e){
    console.log(e)
    var e = e.currentTarget.dataset.id
    var that=this
    var token = app.globalData.token;
    var image = this.data.image;
    var commission_price = this.data.number
    var id = app.globalData.business_id
    var a = String(new Date().getTime())
    var product_sn = "" + id+"PM"+a.substring(8)
    addactivityProduct({
      token:token,
      image: image,
      commission_price: commission_price,
      product_sn: product_sn
    }).then(res=>{
      console.log(res)
      if(res.data.code===666){
        wx.showToast({
          title: res.data.msg,
          icon:"none"
        })
        if(e==1){
          setTimeout(() => {
            wx.redirectTo({
              url: '../outshop/outshop',
            })
          }, 1000)
        }else{
          setTimeout(() => {
            wx.redirectTo({
             url: '../../shopCar/shopCar',
           })
          }, 1000)
        }
       
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  img_look(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      current: that.data.showImages[index], // 当前显示图片的http链接
      urls: that.data.showImages // 需要预览的图片http链接列表
    })
  },
  addImg: function () {
   
    var that = this
    wx.chooseImage({
      count: that.data.count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res)
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          showImages: tempFilePaths,
        })
        getToken().then(res => {
          that.setData({
            token_pic: res.data.data.token,
          })
          that.uplod(tempFilePaths, 20)
        })
      }
    })
   
  },
  uplod: function (obj, num) {
    var that = this
    var showImages = that.data.showImages
    for (var i = 0; i < obj.length; i++) {
      var timestamp = Date.parse(new Date()) + [i]
      var type = obj[i].substring(obj[i].lastIndexOf("."), obj[i].length);
      var keyy = timestamp + type
      wx.uploadFile({
        url: 'https://upload.qiniup.com',
        filePath: obj[i],
        name: 'file', //文件的key 
        header: {
          "Content-Type": "multipart/form-data"
        },
        formData: {
          token: that.data.token_pic,
          key: keyy
        }, //传递参数
        success: function (res) {
          console.log(res)
          var data = JSON.parse(res.data)
          var singleImg = {};
          if (num != 20) {
            that.data.image[num].pic_url = data.key
            that.data.image[num].id = that.data.showImages[num].id
          } else {
            var image = that.data.image;
            var min;
            singleImg.pic_url = data.key
            image.push(singleImg)
            for (var i = 0; i < image.length; i++) {
              for (var j = i; j < image.length; j++) {
                if (parseInt(image[i].pic_url) > parseInt(image[j].pic_url)) {
                  min = image[j].pic_url;
                  image[j].pic_url = image[i].pic_url;
                  image[i].pic_url = min;
                }
              }
            }
           
          }
          that.setData({
            image:[],
            image: that.data.image
          })
         
        }
      })
    }
  },
  //替换img
  changeImg: function (e) {
    var inde = e.currentTarget.dataset.key
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        var showImages = that.data.showImages.splice(inde, 1, tempFilePaths[0])
        that.setData({
          showImages: that.data.showImages
        })
        console.log(that.data.showImages)
        getToken().then(res => {
          that.setData({
            token_pic: res.data.data.token,
          })
          that.uplod(tempFilePaths, inde)
        })
      }
    })
  },
  img_delete(e) {
    var index = e.currentTarget.dataset.index
    var node = this.data.showImages
    this.data.image.splice(index, 1)
    node.splice(index, 1)
    var that = this
    var that_count = 0
    if (15 - that.data.showImages.length > 9) {
      that_count = 9
    } else {
      that_count = 15 - that.data.showImages.length
    }
    this.setData({
      showImages: this.data.showImages,
      count: that_count
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      iphoneX: app.globalData.is_iPhoneX
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})